﻿Imports System.IO
Public Class form_puzzle
    'I got this code for the puzzel from this source, I modified it a bit thanks a lot for your help Eugenio Jacobo Hernandez Valdemar, regards, Linda & Debby from Indonesia
    'Jigsaw in VB.Net
    '
    'A simple jigsaw generator to demonstrate some usefull DrawImage calls
    'and how to manipulate bitmap sections from an image
    '
    'Author: Eugenio Jacobo Hernandez Valdelamar
    '2005

    'cJigsaw class
    'allows to create a new jigsaw (could be a singleton)
    'holds as many pieces as the dimension specified require
    'look at the DrawImage calls to see how to scale, segment and process an image
    Inherits System.Windows.Forms.Form
    Dim xUrutan As Integer
    Public Property urutan As Integer
        Get
            Return xUrutan
        End Get
        Set(ByVal value As Integer)
            xUrutan = value
        End Set
    End Property
    'the jigsaw
    Private js As cJigsaw

    'jigsaw piece bmp
    Private bmp_piece As Bitmap
    Private pieceSel As Boolean

    'selected positions
    Private xo, yo As Integer
    Private xd, yd As Integer

    'image files available
    Private files() As String

    Private score As Integer 'how many images do you complete
    '//////////////////////////////////////////////////////////////////
    Private Sub PictureBox9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox9.Click
        form_beginner.Show()
        Me.Close()
    End Sub


    Private Sub form_puzzle_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        js = New cJigsaw(picBack)
        'images source
        files = Directory.GetFileSystemEntries(Application.StartupPath & "\aksara", "*.jpg")
        score = 0
        newTry() 'lets begin
    End Sub
    Private Sub catchPiece(ByVal x As Integer, ByVal y As Integer)
        Dim ro As Rectangle
        Dim xpos, ypos As Integer
        Dim g As Graphics

        g = Graphics.FromImage(bmp_piece)

        xpos = x * js.pieceSizeW
        ypos = y * js.pieceSizeH
        ro = New Rectangle(xpos, ypos, js.pieceSizeW, js.pieceSizeH)

        Dim rd As New Rectangle(0, 0, js.pieceSizeW, js.pieceSizeH)
        g.DrawImage(picBack.Image, rd, ro, GraphicsUnit.Pixel)

        picPiece.Image = bmp_piece
    End Sub



    Private Sub newTry()
        Dim rand As New Random

        js.Dimension = 2
        js.loadImage(files(rand.Next(0, UBound(files) + 1)))

        js.generateJigSaw()
        js.shuffle()
        js.repaintJigSaw()

        bmp_piece = New Bitmap(js.pieceSizeW, js.pieceSizeH)


        picPiece.Width = js.pieceSizeW
        picPiece.Height = js.pieceSizeH
        picPiece.Visible = False


        timerX.Enabled = True
    End Sub

    Private Sub picBack_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picBack.MouseDown


        Dim i, j As Integer

        xo = e.X \ js.pieceSizeW
        yo = e.Y \ js.pieceSizeH
        picPiece.Visible = True
        catchPiece(xo, yo)
        picPiece.Left = picBack.Left + xo * js.pieceSizeW
        picPiece.Top = picBack.Top + yo * js.pieceSizeH
        pieceSel = True
        js.putBlank(xo, yo)

    End Sub

    Private Sub picBack_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picBack.MouseMove

        If e.Button = MouseButtons.Left And pieceSel = True Then
            picPiece.Left = e.X
            picPiece.Top = e.Y
        End If

    End Sub

    Private Sub picBack_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picBack.MouseUp

        xd = e.X \ js.pieceSizeW
        yd = e.Y \ js.pieceSizeH
        js.movePiece2(xd, yd, xo, yo)
        Dim kelas As New player
        If js.isImgSorted = True Then
            score += 10
            lbScore.Text = score
            MsgBox("Good Job!!")
            newTry()
            kelas.NilaiPuzzle = score
            kelas.skorpuzzle()
            Dim xTotal = 1
            kelas.NilaiPuzzle = score
            kelas.skorpuzzle()
        End If
        picPiece.Visible = False
        pieceSel = False
    End Sub


    Private Sub timerX_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerX.Tick

    End Sub

    Private Sub lbScore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbScore.Click

    End Sub
End Class